<template>
  <div id="wrapper">
    <button @click="print">打印</button>
    <webview src="../../../static/print.html" nodeintegration></webview>
  </div>
</template>

<script>
  import SystemInformation from './LandingPage/SystemInformation'
  const ipcRenderer = require("electron").ipcRenderer;

  export default {
    name: 'landing-page',
    components: { SystemInformation },
    data() {
      return {
        print0: "",
        print1: ""
      }
    },
    mounted() {
      this.getPrinterList()
    },
    methods: {
      open (link) {
        this.$electron.shell.openExternal(link)
      },
      print() {
        const webview = document.querySelector("webview");
        console.log(webview);
        // webview.addEventListener("dom-ready", () => {
        //
        // });
        alert('ping')
        console.log("dom-ready");
        //dom-ready---webview加载完成
        webview.openDevTools(); //这个方法可以打开print.html的控制台
        var arr = ['啊飒飒飒飒飒飒飒飒飒飒飒飒飒飒飒飒飒飒啊啊啊啊啊啊啊啊啊啊'];
        //在send时将arr传递过去
        webview.send("ping", arr); //向webview嵌套的页面响应事件

        webview.addEventListener("ipc-message", event => {
          console.log(event.channel); // Prints "pong" 在此监听事件中接收webview嵌套页面所响应的事件
          if (event.channel == "pong") {
            console.log("通信成功");
            webview.print(
              {
                //是否是静默打印,true为静默打印，false会弹出打印设置框
                silent: true,
                printBackground: true,
                //打印机的名称，this.print1为在getPrinterList()方法中获取到的打印机名字。
                //注意在demo中这是我打印机的设备，在使用本demo时，先去getPrinterList()中找到你使用的打印机
                deviceName: this.print1
              },
              data => {
                //这个回调是打印后的回调事件，data为true就是打印成功，为false就打印失败
                console.log("webview success", data);
              }
            );
          }
        });
      },
      getPrinterList() {
        //使用ipcRenderer与主进程通信，并获取返回值
        ipcRenderer.send("getPrinterList");
        //监听主线程获取到打印机列表后的回调
        ipcRenderer.once("getPrinterList", (event, data) => {
          console.log('data', data)
          //data就是打印机列表
          this.print0 = data[3].name;
          this.print1 = data[0].name;
          console.log(data[3].name);
          console.log(data[0].name);
        });
      },
    }
  }
</script>

<style lang="stylus">
  .left-side{
    background red
  }
</style>
<style lang="scss">
  @import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro');

  .left-side{
    .title{
      font-size: 50px;
    }
  }
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body { font-family: 'Source Sans Pro', sans-serif; }

  #wrapper {
    background:
      radial-gradient(
        ellipse at top left,
        rgba(255, 255, 255, 1) 40%,
        rgba(229, 229, 229, .9) 100%
      );
    height: 100vh;
    padding: 60px 80px;
    width: 100vw;
  }

  #logo {
    height: auto;
    margin-bottom: 20px;
    width: 420px;
  }

  main {
    display: flex;
    justify-content: space-between;
  }

  main > div { flex-basis: 50%; }

  .left-side {
    display: flex;
    flex-direction: column;
  }

  .welcome {
    color: #555;
    font-size: 23px;
    margin-bottom: 10px;
  }

  .title {
    color: #2c3e50;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 6px;
  }

  .title.alt {
    font-size: 18px;
    margin-bottom: 10px;
  }

  .doc p {
    color: black;
    margin-bottom: 10px;
  }

  .doc button {
    font-size: .8em;
    cursor: pointer;
    outline: none;
    padding: 0.75em 2em;
    border-radius: 2em;
    display: inline-block;
    color: #fff;
    background-color: #4fc08d;
    transition: all 0.15s ease;
    box-sizing: border-box;
    border: 1px solid #4fc08d;
  }

  .doc button.alt {
    color: #42b983;
    background-color: transparent;
  }
</style>
